jsx3.Class.defineClass(
	"tibco.admin.monitormanage.general.PollingHandler",                   // the full name of the class to create
    jsx3.lang.Object, //  extends
	null,  // does not implement an interface
	function(handler) {
		handler.GENERAL_POLLING_HANDLER_ID = "GENERAL_POLLING_HANDLER_ID";
		handler.pollSubject = "com.tibco.admin.monitormanage.general.PollingHandler.poll";
		handler.registerSubject = "com.tibco.admin.monitormanage.general.PollingHandler.register";
		handler.pauseSubject = "com.tibco.admin.monitormanage.general.PollingHandler.pause";
		handler.unregisterSubject = "com.tibco.admin.monitormanage.general.PollingHandler.unregister";
		
		handler.pollingInterval = 60 * 1000;
		
		handler.prototype.pollingHandlerId;
		
		handler.prototype.registeredScreens;
		handler.prototype.registeredObjCache;
		
		handler.prototype.currentScreenId;
		handler.prototype.pollingStarted;
		handler.prototype.isPaused;

		/**
                 *  Init method used for creating a polling handler instance identified by the pollingHandlerId.
                 *  @param pollingHandlerId the id of the polling handler instance
                 */
		handler.prototype.init = function(pollingHandlerId) {
			this.jsxsuper();
			
			this.pollingHandlerId = pollingHandlerId;
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.pollSubject, this.handlePoll, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.registerSubject, this.handleRegistration, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.pauseSubject, this.handlePause, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.unregisterSubject, this.handleUnregistration, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe("com.tibco.admin.monitormanage.general.NavigationHandler.navigation", this.handleMainPageNavigation, this);
            this.registeredScreens = new jsx3.util.List();
            this.registeredObjCache = new jsx3.util.List();
            this.pollingStarted = false;
            this.isPaused = false;
            this.currentScreenId = "tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard.dashboardScreen";
		}
        
        /**
         *  Starts the polling
         */
        handler.prototype.startPolling = function(pollingHandlerId) {
        	var timeoutFunction = "tibco.admin.monitormanage.general.PollingHandler.publishPollMessage('" + pollingHandlerId + "');";
        	setTimeout(timeoutFunction,handler.pollingInterval);
        }
        
        /**
         *  Not for public consumption
         */
        handler.prototype.handleMainPageNavigation = function(subject, message, pollingHandler) {
			var screenId = message.screenId;
			pollingHandler.currentScreenId = screenId;
		}
		
        /**
         *  Not for public consumption
         */
	handler.prototype.handlePause = function(subject, message, pollerObj) {
	    pollerObj.isPaused = message.isPaused;
	}
        
        /**
         *  Not for public consumption
         */
        handler.prototype.handlePoll = function(subject, message, pollerObj) {
        	var pollId = message.pollingHandlerId;
        	if(!pollerObj.isPaused)
        	{
	        	if(pollId == pollerObj.pollingHandlerId)
	        	{
	        		var screenIdx = pollerObj.registeredScreens.indexOf(pollerObj.currentScreenId);
	        		if(screenIdx != -1)
	        		{
	        			var registeredObjList = pollerObj.registeredObjCache.get(screenIdx);
	        			for(i = 0; i < registeredObjList.size(); i++)
	        			{
	        				var registeredObj = registeredObjList.get(i);
	        				var callbackTarget = registeredObj.callbackTarget;
	        				var callbackFunction = registeredObj.callbackFunction;
	        				callbackFunction.call(callbackTarget);
	        			}
	        		}
	        	}
        	}
        	pollerObj.startPolling(pollId);
        }
        
        /**
         *  Not for public consumption
         */
        handler.prototype.handleRegistration = function(subject, message, pollerObj) {
        	var pollId = message.pollingHandlerId;
        	if(pollId == pollerObj.pollingHandlerId)
        	{
        		var registeredObj = handler.createRegisteredObjCache(message);
        	
        		var screenId = message.screenId;
        		var screenIdx = pollerObj.registeredScreens.indexOf(screenId);
        		if(screenIdx == -1)
        		{
        			// there is nothing registered for this screen yet
        			pollerObj.registeredScreens.add(screenId);
        			var registeredObjList = new jsx3.util.List();
        			registeredObjList.add(registeredObj);
        			pollerObj.registeredObjCache.add(registeredObjList);
        		}
        		else
        		{
        			// add the registered obj to the existing registeredObjList
        			var registeredObjList = pollerObj.registeredObjCache.get(screenIdx);
        			registeredObjList.add(registeredObj);
        		}
        	
        		if(!pollerObj.pollingStarted)
        		{ 
        			pollerObj.startPolling(pollerObj.pollingHandlerId); 
        			pollerObj.pollingStarted = true;
        		}
        	}
        }
        
        /**
         *  Not for public consumption
         */
        handler.prototype.handleUnregistration = function(subject, message, pollerObj) {
        	if(message.unregisterAll)
        	{
        		// The perspective must have been switched so reset the polling handler
        		pollerObj.registeredScreens.clear();
        		pollerObj.registeredObjCache.clear();
	            pollerObj.isPaused = true;
	            pollerObj.currentScreenId = "tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard.dashboardScreen";
        	}
        }
        
        /**
         *  Not for public consumption
         */
        handler.createRegisteredObjCache = function(message) {
        	var registeredObj = new Object();
        	registeredObj.callbackTarget = message.callbackTarget;
        	registeredObj.callbackFunction = message.callbackFunction;
        	return registeredObj;
        }
        
        /**
         *  Not for public consumption
         */
        handler.publishPollMessage = function(pollingHandlerIdVal) {
        	var message = { pollingHandlerId:pollingHandlerIdVal };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.pollSubject, message);
        }
        
	/**
         *  Registers the callbackFunction to be called on the callbackTarget when the polling interval has expired.
         *  The callbackFunction is called ONLY IF the currently displayed screen matches the screenId specified.
         *
         *  @param pollingHandlerIdVal specifies which PollingHandler class instance should be handling the polling
         *  @param callbackTargetVal the target object which the callback function will be invoked on
         *  @param callbackFunctionVal the function which will be invoked on the callbackTargetVal object
         *  @param screenIdVal the screen identifier which indicates which screen needs to be displayed in order 
         *         for the callbackFunction to be invoked
         */
        handler.register = function(pollingHandlerIdVal, callbackTargetVal, callbackFunctionVal, screenIdVal) {
			var message = { pollingHandlerId:pollingHandlerIdVal, callbackTarget:callbackTargetVal, callbackFunction:callbackFunctionVal, screenId:screenIdVal };
			tibco.admin.monitormanage.general.PageBusUtils.publish(handler.registerSubject, message);
        }
        
	/**
         *  Pauses the polling until "unpausePolling" is called
         */
        handler.pausePolling = function() {
        	var message = { isPaused:true };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.pauseSubject, message);
        }
        
	/**
         *  Unpauses the polling
         */
        handler.unpausePolling = function() {
        	var message = { isPaused:false };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.pauseSubject, message);
        }
        
	/**
         *  Unregisters all of the registered callbackFunctions
         */
        handler.unregisterAll = function() {
        	var message = { unregisterAll:true };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.unregisterSubject, message);
        }

	}
);